package net.milkdrops;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.*;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class CleanResGUI extends javax.swing.JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1600532762007579488L;
	private JMenuItem helpMenuItem;
	private JMenu jMenu5;
	private JMenuItem deleteMenuItem;
	private JButton jButton;
	private JList unUsedResourcesList;
	private JMenuItem deleteAllMenuItem;
	private JMenu jMenu4;
	private JMenuItem exitMenuItem;
	private JSeparator jSeparator2;
	private JMenuItem openFileMenuItem;
	private JMenu jMenu3;
	private JMenuBar jMenuBar1;
	
	private CleanRes mCleanRes;
	
	private String sPath;
	private boolean bStart = false;
	
	private static final String VERSION_STR = "0.1";

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				CleanResGUI inst = new CleanResGUI();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public CleanResGUI() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			BorderLayout thisLayout = new BorderLayout();
			getContentPane().setLayout(thisLayout);
			{
				unUsedResourcesList = new JList();
				unUsedResourcesList.setListData(new String[] {"Open Android Project Directory"});
				unUsedResourcesList.setLayoutOrientation(JList.VERTICAL);
				JScrollPane listScroller = new JScrollPane(unUsedResourcesList);
				getContentPane().add(listScroller, BorderLayout.CENTER);
			}
			{
				jButton = new JButton("Start");
				jButton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent arg0) {
						if (bStart) {
							mCleanRes.setbCancel(true);
							jButton.setText("Start");
							bStart = false;
							return;
						}
						if (sPath == null && sPath.length() <= 0) {
							JOptionPane.showMessageDialog(CleanResGUI.this, "Select directory",
				                       "Error", JOptionPane.ERROR_MESSAGE);
						}
						
						bStart = true;
						jButton.setText("Cancel");
						
						new Thread() {

							@Override
							public void run() {
								// TODO Auto-generated method stub
								mCleanRes.removeResources(CleanResGUI.this);
							}
							
						}.start();
						
					}
					
				});
				getContentPane().add(jButton, BorderLayout.SOUTH);
			}
			setSize(400, 300);
			{
				jMenuBar1 = new JMenuBar();
				setJMenuBar(jMenuBar1);
				{
					jMenu3 = new JMenu();
					jMenuBar1.add(jMenu3);
					jMenu3.setText("File");
					{
						openFileMenuItem = new JMenuItem();
						jMenu3.add(openFileMenuItem);
						openFileMenuItem.setText("Open");
						openFileMenuItem.addActionListener(new ActionListener() {

							@Override
							public void actionPerformed(ActionEvent e) {
								if(e.getActionCommand().equals("Open")) {
									JFileChooser jFileChooser = new JFileChooser();
									jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
									int returnVal = jFileChooser.showOpenDialog(CleanResGUI.this);
									if(returnVal == JFileChooser.APPROVE_OPTION) {
										sPath = jFileChooser.getSelectedFile().getAbsolutePath();
										if (mCleanRes == null) {
											mCleanRes = new CleanRes(sPath);
										} else {
											mCleanRes.setRootPath(sPath);
										}
									}
								}
							}
							
						});
					}
					{
						jSeparator2 = new JSeparator();
						jMenu3.add(jSeparator2);
					}
					{
						exitMenuItem = new JMenuItem();
						jMenu3.add(exitMenuItem);
						exitMenuItem.setText("Exit");
						exitMenuItem.addActionListener(new ActionListener() {

							@Override
							public void actionPerformed(ActionEvent arg0) {
								// TODO Auto-generated method stub
								CleanResGUI.this.dispose();
							}
							
						});
					}
				}
				{
					jMenu4 = new JMenu();
					jMenuBar1.add(jMenu4);
					jMenu4.setText("Edit");
					{
						deleteMenuItem = new JMenuItem();
						jMenu4.add(deleteMenuItem);
						deleteMenuItem.setText("Delete");
						deleteMenuItem.addActionListener(new ActionListener() {

							@Override
							public void actionPerformed(ActionEvent arg0) {
								// TODO Auto-generated method stub
								mCleanRes.delete(unUsedResourcesList.getSelectedIndices());
								unUsedResourcesList.setListData(mCleanRes.getResult());
								unUsedResourcesList.invalidate();
							}
							
						});
					}
					{
						deleteAllMenuItem = new JMenuItem();
						jMenu4.add(deleteAllMenuItem);
						deleteAllMenuItem.setText("Delete All");
						deleteAllMenuItem.addActionListener(new ActionListener() {

							@Override
							public void actionPerformed(ActionEvent arg0) {
								// TODO Auto-generated method stub
								mCleanRes.deleteAll();
								unUsedResourcesList.setListData(mCleanRes.getResult());
								unUsedResourcesList.invalidate();
							}
							
						});
					}
				}
				{
					jMenu5 = new JMenu();
					jMenuBar1.add(jMenu5);
					jMenu5.setText("Help");
					{
						helpMenuItem = new JMenuItem();
						jMenu5.add(helpMenuItem);
						helpMenuItem.setText("Help");
						helpMenuItem.addActionListener(new ActionListener() {

							@Override
							public void actionPerformed(ActionEvent arg0) {
								JOptionPane.showMessageDialog(CleanResGUI.this, "Version v"+VERSION_STR+"\nhttp://milkdrops.net",
					                       "CleanRes", JOptionPane.INFORMATION_MESSAGE);
							}
							
						});
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void setResult(ArrayList<File> list) {
		bStart = false;
		jButton.setText("Start");
		String[] fileList = new String[list.size()];
		for (int i=0; i<list.size(); i++) {
			File file = list.get(i);
			fileList[i] = file.getName();
		}
		if (fileList.length == 0) {
			fileList = new String[1];
			fileList[0] = "No result";
		}
		unUsedResourcesList.setListData(fileList);
		unUsedResourcesList.invalidate();
	}
	
	public void setProgress(File file) {
		String[] pathList = new String[3];
		pathList[0] = "Processing.. : " + sPath;
		pathList[1] = file.getParent().replaceAll(sPath, "");
		pathList[2] = file.getName();
		unUsedResourcesList.setListData(pathList);
		unUsedResourcesList.invalidate();
	}

}
